package controller

import (
	"dta-apk/query/request"
	"dta-apk/query/response"
	"dta-apk/service"
	"dta-apk/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

/*
 应用模块控制器
*/

// 全部应用
func GetAppList(ctx *gin.Context) *response.ResultRes {
	result := service.GetAppList()
	return response.NewSuccess(result)
}

// 创建应用
func CreateApp(ctx *gin.Context) *response.ResultRes {
	var addQuery request.AppAddQuery
	if err := ctx.BindJSON(&addQuery); err != nil {
		utils.Error(err)
		return response.NewParamFailed()
	}
	result, mess := service.CreateApp(addQuery)
	if !result {
		return response.NewFailed(500, mess)
	}
	return response.NewSuccessMsg(nil, mess)
}

// 获取应用
func GetApp(ctx *gin.Context) *response.ResultRes {
	appId := ctx.Query("appId")
	if appId == "" {
		return response.NewParamFailed()
	}
	app := service.GetApp(appId)
	return response.NewSuccess(app)
}

// 更新应用
func UpdateApp(ctx *gin.Context) *response.ResultRes {
	var query request.AppUpdateQuery
	if err := ctx.BindJSON(&query); err != nil {
		utils.Error(err)
		return response.NewParamFailed()
	}
	result, mess := service.UpdateApp(query)
	if !result {
		return response.NewFailed(500, mess)
	}
	return response.NewSuccessMsg(nil, mess)
}

// 更新应用资料
func UpdateAppInfo(ctx *gin.Context) *response.ResultRes {
	// 请求参数
	var apiInfoQuery request.AppInfo
	if err := ctx.ShouldBind(&apiInfoQuery); err != nil {
		utils.Error(err)
		return response.NewFailed(http.StatusBadRequest, "表单数据无效")
	}
	result := service.UpdateAppInfo(apiInfoQuery)
	if !result {
		return response.NewRequestFailed()
	}
	return response.NewSuccess(nil)
}

// 获取应用资料
func GetAppInfo(ctx *gin.Context) *response.ResultRes {
	appId := ctx.Query("appId")
	if appId == "" {
		return response.NewParamFailed()
	}
	appInfo := service.GetAppInfoSync(appId)
	return response.NewSuccess(appInfo)
}

// 同步应用平台资料
func SyncAppInfo(ctx *gin.Context) *response.ResultRes {
	var appQuery request.AppQuery
	error := ctx.ShouldBindJSON(&appQuery)
	if error != nil {
		utils.Error(error)
		return response.NewParamFailed()
	}
	// 同步逻辑
	result := service.SyncAppInfo(appQuery.AppId)
	if result {
		return response.NewSuccess(nil)
	}
	return response.NewFailed(500, "同步失败")
}

// 获取审核状态信息
func GetCheckInfo(ctx *gin.Context) *response.ResultRes {
	appId := ctx.Query("appId")
	if appId == "" {
		return response.NewParamFailed()
	}
	checkInfo := service.GetCheckInfo(appId)
	return response.NewSuccess(checkInfo)

}

// 应用发布
func PublishApp(ctx *gin.Context) *response.ResultRes {
	var query request.AppPublishQuery
	error := ctx.ShouldBind(&query)
	if error != nil {
		utils.Error(error)
		return response.NewParamFailed()
	}
	isS := service.PublishApp(query)
	if !isS {
		return response.NewMsgFailed("发布失败")
	}
	return response.NewSuccess(nil)
}

// 同步各平台的审核详情
func SyncPublishCheckInfo(ctx *gin.Context) *response.ResultRes {
	appId := ctx.Query("appId")
	if appId == "" {
		return response.NewParamFailed()
	}
	result := service.SyncCheckInfo(appId)
	if result {
		return response.NewSuccess(nil)
	}
	return response.NewMsgFailed("同步失败")
}
